#!/usr/bin/env python
# -*- coding: UTF-8 -*-

try:
	import pygame
	import sys
	from pygame.locals import *
	from creature import *
	import lib.utils
except ImportError, err:
	print "Could not load %s module." % (err)
	sys.exit(2)

class Units(pygame.sprite.DirtySprite):
	"""
	Object for the Units overlay
	"""
	def __init__(self, field, layers):
		pygame.sprite.DirtySprite.__init__(self)
		self.rect = field.rect
		self.layers = layers
		self.field = field

	
		self.unit_cache = utils.UnitImageCache()

		self.image = pygame.Surface(self.field.image.get_size(), SRCALPHA, 32).convert_alpha()

		self.highlight = False
		self.highlight_list = []

		self.unit = []
		for x in xrange(self.field.map_w):
			self.unit.append([])
			for y in xrange(self.field.map_h):
				self.unit[x].append(None)

		self.focused_unit = None
		self.selected_unit = None

		#testing units
		x = 12
		y = 10
		
		true_x = x * 48
		true_y = y * 48
		
		self.unit[x][y] = Creature(true_x, true_y, self.layers, self.field, self.unit_cache)
		self.unit[x+1][y] = Creature(true_x + 48, true_y, self.layers, self.field, self.unit_cache)

	def walk_unit(self, dest):
		x ,y = dest
		if self.test_highlight(dest) == False and (self.unit[x][y] == None or self.unit[x][y] == self.selected_unit):
			self.focused_unit.focus()
			self.focused_unit = self.selected_unit.focus()
			self.old_highlight_list = self.highlight_list
			self.selected_unit.walk(dest)
			return True
		return False

	def confirm_move(self):
		"""
		Confirms movement of unit. Returns false if unit still moving
		"""
		if self.selected_unit.confirm_move():
			self.disable_highlight()
			self.selected_unit = None

			return True
		return False


		

	def cancel(self):
		self.set_highlight(self.old_highlight_list)
		self.selected_unit.cancel()

	def focus_unit(self, x, y):
		"""
		Highlights the unit at x, y
		Only one unit (Change to group of units later) is highlighted at one time
		"""
		if self.focused_unit != None:
			self.focused_unit.focus()
		self.focused_unit = self.unit[x][y].focus()

	def select_unit(self, unit = None):
		"""
		Selects the unit at x, y
		"""
		self.selected_unit = unit


	def __getitem__(self, y):
		"""
		Allows listing of units in class directly
		"""
		return self.unit[y]

	def update(self):
		"""
		Checks if Battlefield has moved, then moves overlay with it
		"""
		if self.rect != self.field.rect:
			self.rect = self.field.rect
			self.dirty = 1

	def set_highlight(self, list):
		"""
		Sets highlighting based on list of tile co-ords (list)
		"""

		self.image.fill((0, 0, 0, 150))
		for (x, y) in list:
			self.image.fill((0, 0, 0, 0), ((x + 1) * 48, (y + 1) * 48, 48, 48))
		self.highlight = True
		self.highlight_list = list
		self.dirty = 1


	def test_highlight(self, pos):
		"""
		Tests if Tile Co-ordinate (pos) is highlighted
		Returns False if highlighted (ie. transparent)
		"""
		if pos in self.highlight_list:
			return False
		else:
			return True

	def disable_highlight(self):
		"""
		Disables highlighting
		"""
		self.highlight = False
		self.selected_unit.select()
		self.selected_unit = False
		self.image.fill((0, 0, 0, 0))
		self.dirty = 1


		


